<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="title">
        <el-input v-model="listQuery.title" size="small" placeholder="标题" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="wzmediaid">
        <el-input v-model="listQuery.wzmediaid" size="small" placeholder="MediaID" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
        <el-divider direction="vertical" />
        <el-button
          size="small"
          icon="el-icon-download"
          :loading="syncWxMaterialsLoading"
          :disabled="syncWxMaterialsLoading"
          @click="syncWxMaterials"
        >获取微信素材</el-button>
        <!-- <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toMaterialNew"
        >新建素材</el-button>
        <el-button
          size="small"
          type="warning"
          icon="el-icon-document-copy"
          @click="copyMaterial"
        >复制新建</el-button> -->
        <!-- <el-button
          size="small"
          icon="el-icon-refresh"
          @click="syncMaterials"
        >批量同步</el-button> -->
        <!-- <el-button
          size="small"
          type="danger"
          icon="el-icon-delete"
          @click="removeMaterials"
        >批量删除</el-button> -->
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        type="selection"
        width="50"
        align="center"
      /> -->
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="MediaID">
        <template slot-scope="scope">
          {{ scope.row.wzmediaid }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.createdtime | moment('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="治疗领域" align="center">
        <template slot-scope="scope">
          <el-tag
            v-for="item in scope.row.fieldList"
            :key="item.id"
            effect="plain"
            size="mini"
          >{{ item.name }}</el-tag>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="科室">
        <template slot-scope="scope"></template>
      </el-table-column>
      <el-table-column label="渠道">
        <template slot-scope="scope"></template>
      </el-table-column>
      <el-table-column label="产品">
        <template slot-scope="scope"></template>
      </el-table-column> -->
      <!-- <el-table-column label="是否同步" width="80" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.syncstate === 1" class="el-icon-check text-success" />
          <i v-else class="el-icon-close text-warning" />
        </template>
      </el-table-column> -->

      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="toMaterialShow(scope.row.id)"
          >详情</el-button>
          <!-- <el-button
            size="mini"
            @click="toMaterialEdit(scope.row.id)"
          >编辑</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getMaterials,
  removeMaterials,
  copyMaterial,
  syncWxMaterials,
  syncMaterials
  // onlineMaterials,
  // getWechatMaterials
} from '@/api/materials'
import Pagination from '@/components/Pagination'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'MaterialsList',
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        title: formatQuery(this.$route.query.title, false),
        wzmediaid: formatQuery(this.$route.query.wzmediaid, false),
        page: 1,
        size: 30
      },
      selectedIds: [],
      syncWxMaterialsLoading: false
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getMaterials(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    handleSelectionChange(val) {
      console.log(val)
      this.selectedIds = this._.map(val, 'id')
      console.log(this.selectedIds)
    },
    copyMaterial() {
      copyMaterial().then(response => {
        // console.log(response)
      }).catch(err => {
        console.log(err)
      })
    },
    syncWxMaterials() {
      this.syncWxMaterialsLoading = true
      syncWxMaterials({
        type: 'news'
      }).then(response => {
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: response.msg
          })
          this.handleFilter()
        } else {
          this.$message({
            type: 'info',
            message: '获取失败，请稍后再试'
          })
        }
        this.syncWxMaterialsLoading = false
      }).catch(err => {
        console.log(err)
        this.syncWxMaterialsLoading = false
      })
    },
    syncMaterials() {
      this.syncWxMaterialsLoading = true
      const ids = this.selectedIds.join(',')
      syncMaterials({ ids }).then(response => {
        console.log(response)
        this.syncWxMaterialsLoading = false
      }).catch(err => {
        console.log(err)
        this.syncWxMaterialsLoading = false
      })
    },
    removeMaterials() {
      const ids = this.selectedIds.join(',')
      this.$confirm('此操作将永久删除该素材, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMaterials({ ids }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    toMaterialEdit(id) {
      this.$router.push(`/materials/edit/${id}`)
    },
    toMaterialNew(id) {
      this.$router.push('/materials/new')
    },
    toMaterialShow(id) {
      this.$router.push(`/materials/list/${id}`)
    }
  }
}
</script>
